<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        body{
            position: relative;
            background-color: #000000;
        }
        div{
            width: 500px;
            height: 500px;
            position: absolute;
            top: 50%;
            left: 50%;
            background-color: aliceblue;
        }
    </style>
</head>
<body>
    <div id="box">
        <input type="button" value="换色" id="btn">
    </div>
    
    <script>
        var a = document.getElementById('box')
        var b = document.getElementById('btn')
        function randomNum(n, m) {
            var max, min;
            max = n > m ? n : m;
            min = n < m ? n : m;
            return parseInt(Math.random() * (max - min + 1) + min);
        }
        
        btn.onclick=function(){ 
         a.style.backgroundColor =  '#' +(randomNum(0,16777215).toString(16)); 
          
        }
    </script>
</body>
</html>